﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Pantry.Commons;
using Pantry.Entity;
using Pantry.DataAccess;
using System.Data.SqlClient;

namespace Pantry.Business
{
    public sealed class DirectorateBus : IDisposable
    {
        private readonly DirectorateDao _directorateDao;
        /// <summary>
        /// Initializes a new instance of the <see cref="DirectorateBus"/> class.
        /// </summary>
        public DirectorateBus()
        {
            _directorateDao = new DirectorateDao();
        }
        /// <summary>
        /// Lists all directorates.
        /// </summary>
        /// <param name="organisationId">The organisation id.</param>
        /// <returns></returns>
        public DataTable ListAllDirectorates(int organisationId)
        {
            try
            {
                return _directorateDao.ListAllDirectorates(organisationId);
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return null;
            }
        }
        /// <summary>
        /// Creates the directorate.
        /// </summary>
        /// <param name="directorate">The directorate.</param>
        /// <returns></returns>
        public bool CreateDirectorate(Directorate directorate)
        {
            try
            {
                var result = _directorateDao.CreateDirectorate(directorate);
                return (result != 0);
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return false;
            }
        }
        /// <summary>
        /// Updates the directorate.
        /// </summary>
        /// <param name="directorate">The directorate.</param>
        /// <returns></returns>
        public bool UpdateDirectorate(Directorate directorate)
        {
            try
            {
                var result = _directorateDao.UpdateDirectorate(directorate);
                return (result != 0);
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return false;
            }
            catch(ArgumentNullException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return false;
            }
        }
        /// <summary>
        /// Gets the directorate by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public Directorate GetDirectorateById(int id)
        {
            try
            {
                return _directorateDao.GetDirectorateById(id);
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return null;
            }
        }
        /// <summary>
        /// Ins the active directorate.
        /// </summary>
        /// <param name="directorateId">The directorate id.</param>
        /// <returns></returns>
        public bool InactiveDirectorate(int directorateId)
        {
            try
            {
                int result = _directorateDao.ChangeDirectorateStatus(directorateId);
                return (result != 0);
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return false;
            }
        }
        /// <summary>
        /// Checks the directorate name is exist.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public bool CheckDirectorateNameIsExist(string name)
        {
            try
            {
                int result = _directorateDao.CheckDirectorateNameIsExist(name);
                return (result != 0);
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return false;
            }
        }
        /// <summary>
        /// Makes the directorate is active.
        /// </summary>
        /// <param name="directorateId">The directorate id.</param>
        /// <returns></returns>
        public bool MakeDirectorateIsActive(int directorateId)
        {
            try
            {
                int result = _directorateDao.MakeDirectorateIsActive(directorateId);
                return (result != 0);
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return false;
            }
        }

        #region Disposable
        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                _directorateDao.Dispose();
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}
